﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using MySql.Data.MySqlClient;

namespace Knife.Core.DataBase
{
    public class DapperHelper
    {
        public static string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MainConnectString"].ToString();

        public static List<T> Query<T>(string sql, object param)
        {
            List<T> list = new List<T>();
            using (MySqlConnection conn = new MySqlConnection(ConnectionString))
            {
                list = conn.Query<T>(sql, param).ToList();
            }
            return list;
        }

        public static T QuerySingle<T>(string sql, object param)
        {
            using (MySqlConnection con = new MySqlConnection(ConnectionString))
            {
                return con.QuerySingle<T>(sql, param);
            }
        }

        public static T ExecuteScalar<T>(string sql, object param)
        {
            using (MySqlConnection con = new MySqlConnection(ConnectionString))
            {
                return con.ExecuteScalar<T>(sql, param); ;
            }
        }

        public static int ExecuteNonQuery(string sql, object param)
        {
            int res = 0;
            using (MySqlConnection con = new MySqlConnection(ConnectionString))
            {
                res = con.Execute(sql, param);
            }
            return res;
        }

        public static int ExecuteNonQuery<T>(string sql, T entity)
        {
            int res = 0;
            using (MySqlConnection con = new MySqlConnection(ConnectionString))
            {
                res = con.Execute(sql, entity);
            }
            return res;
        }
    }
}
